package vn.com.vtv.weather;

import vn.com.vtv.R;
import android.annotation.TargetApi;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.util.Log;
import android.widget.RemoteViews;


public class WidgetProvider extends AppWidgetProvider {
	private static final String SYNC_CLICKED = "automaticWidgetSyncButtonClick";
	private static final String SETT_CLICKED = "automaticWidgetSettButtonClick";
	public static final String SYNC_COMPLETE = "updateComplete";
	public static final String WIDGET_CLICKED = "listItemClicked";

	Handler mHandler = new Handler();
	public static WidgetProvider _provider = null;
	public static Context _context;
	public static AppWidgetManager _manager;
	public static int[] _ids;

	/*
	 * this method is called every 30 mins as specified on widgetinfo.xml this
	 * method is also called on every phone reboot
	 */
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		if (null == context)
			context = _context;
		if (null == appWidgetManager)
			appWidgetManager = _manager;
		if (null == appWidgetIds)
			appWidgetIds = _ids;

		_provider = this;
		_context = context;
		_manager = appWidgetManager;
		_ids = appWidgetIds;

		final int N = appWidgetIds.length;
		/*
		 * int[] appWidgetIds holds ids of multiple instance of your widget
		 * meaning you are placing more than one widgets on your homescreen
		 */
		for (int i = 0; i < N; ++i) {
			RemoteViews remoteViews = updateWidgetListView(context, appWidgetIds[i]);
			appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
		}
	}

	static int randomNumber = 0;

	@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
	private RemoteViews updateWidgetListView(Context context, int appWidgetId) {

		// which layout to show on widget
		RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

		// RemoteViews Service needed to provide adapter for ListView
		Intent svcIntent = new Intent(context, WidgetService.class);
		// passing app widget id to that RemoteViews Service
		svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
		svcIntent.putExtra("random", randomNumber);
		randomNumber++;
		// setting a unique Uri to the intent
		// don't know its purpose to me right now
		svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
		// setting adapter to listview of the widget
		remoteViews.setRemoteAdapter(R.id.listViewWidget, svcIntent);

		// setting an empty view in case of no data
		remoteViews.setEmptyView(R.id.listViewWidget, R.id.empty_view);

		// set onClick Button
		remoteViews.setOnClickPendingIntent(R.id.btn_refresh, getPendingClick(context, SYNC_CLICKED));
		remoteViews.setOnClickPendingIntent(R.id.btn_setting, getPendingClick(context, SETT_CLICKED));
		// remoteViews.setOnClickPendingIntent(R.id.listViewWidget,
		// getPendingClick( context , WIDGET_CLICKED));
		remoteViews.setTextViewText(R.id.btn_refresh, "");
		
		remoteViews.setTextViewText(R.id.tv_address, VTVApplication.prefs.getString(VTVApplication.KEY_CITY, ""));
		int bgId = VTVApplication.prefs.getInt(VTVApplication.KEY_BGID, R.drawable.bg_00);
		remoteViews.setInt(R.id.layout_bg, "setBackgroundResource", bgId);
		return remoteViews;
	}

	public PendingIntent getPendingClick(Context context, String action) {
		Intent intent = new Intent(context, getClass());
		intent.setAction(action);
		return PendingIntent.getBroadcast(context, 0, intent, 0);
	}

	@Override
	public void onReceive(final Context context, Intent intent) {
		super.onReceive(context, intent);
		AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
		final RemoteViews remoteViews;
		ComponentName watchWidget;

		remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
		watchWidget = new ComponentName(context, WidgetProvider.class);
		if (SYNC_CLICKED.equals(intent.getAction())) {
			if (null != _provider)
				_provider.onUpdate(null, null, null);
		} else if (SYNC_COMPLETE.equals(intent.getAction())) {
			remoteViews.setTextViewText(R.id.btn_refresh, "");
		} else if (WIDGET_CLICKED.equals(intent.getAction())) {
			context.startActivity(new Intent(context, MainActivity.class));
		} else if ("android.appwidget.action.APPWIDGET_UPDATE".equals(intent.getAction())) {
			remoteViews.setTextViewText(R.id.btn_refresh, "");
		} else if(SETT_CLICKED.equals(intent.getAction())){
			Intent i = new Intent(context,MainActivity.class);
			i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(i);
		}
		Log.e("Status Widget", "action: " + intent.getAction());
		appWidgetManager.updateAppWidget(watchWidget, remoteViews);
	}
}
